package dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DataProvider {

	private static final String USERNAME = "username";
	private static final String PASSWORD = "password";
	private static final String HOST = "host";
	private static final String PORT = "port";
	private static final String DATABASE_NAME = "dbname";

	public static Connection getConnection() {

		Connection con = null;
		try {
			Properties prop = new Properties();
			// load a properties file
			prop.load(new FileInputStream("resource/config.properties"));
			String username = prop.getProperty(USERNAME);
			String password = prop.getProperty(PASSWORD);
			String host = prop.getProperty(HOST);
			String port = prop.getProperty(PORT);
			String dbName = prop.getProperty(DATABASE_NAME);				

			/** Create a variable for the connection URL string. **/
			String connectionUrl = "jdbc:mysql://" + host + ":" + port
					+ "/" + dbName;
			
			System.out.println("connectionUrl:" + connectionUrl);

			/** Register mySQL server driver and establish the connection. **/
			Class.forName("com.mysql.jdbc.Driver");

			/** Get connection **/
			con = DriverManager
					.getConnection(connectionUrl, username, password);

		} catch (SQLException ex) {
			ex.printStackTrace();
			return null;
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
			return null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return con;
	}
	
}
